package cn.xfdzx.android.apps.shop.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.GuessLikeAdapter;
import cn.xfdzx.android.apps.shop.adapter.OftenBuyAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.BuyOftenGoodsBeen;
import cn.xfdzx.android.apps.shop.bean.GetJieguoBeen;
import cn.xfdzx.android.apps.shop.bean.PaySuccessfulBeen;
import cn.xfdzx.android.apps.shop.bean.PosterBeen;
import cn.xfdzx.android.apps.shop.bean.UserInfoBean;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.MyInterestGoodContract;

import cn.xfdzx.android.apps.shop.fragment.OneFragment;
import cn.xfdzx.android.apps.shop.presenter.MyInterestGoodPresenter;

import cn.xfdzx.android.apps.shop.util.ItemOffsetDecoration;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;

import static cn.xfdzx.android.apps.shop.activity.MainActivity.TYPE_IS_RETAIL;

/**
 * 新的支付成功页面，下方猜你喜欢接口调用和我的页面一致
 * */
public class PaySuccessfulNewActivity extends BaseMvpActivity<MyInterestGoodPresenter> implements MyInterestGoodContract.View {

    @BindView(R.id.rv_list_like)
    RecyclerView rvListLike;
    @BindView(R.id.ll_state)
    LinearLayout llState;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.view_green_back)
    View view_green_back;
    @BindView(R.id.tv_total_money)
    TextView tvTotalMoney;
    @BindView(R.id.tv_expect_time)
    TextView tvExpectTime;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    private RelativeLayout.LayoutParams item_bar;
    private int page = 1;
    private int limit = Constants.LIMIT;
    private OftenBuyAdapter oftenBuyAdapter;//我常买的物品
    private List<BuyOftenGoodsBeen.DataBean> mList = new ArrayList<>();
    private String type="0";
    @Override
    public int getLayoutId() {
        return R.layout.activity_pay_successful;
    }

    @Override
    public void initView() {
        initHigh();
        StatusBarUtil.setLightStatusBar(this, false, true);

        mPresenter = new MyInterestGoodPresenter();
        mPresenter.attachView(this);

        rvListLike.setItemAnimator(new DefaultItemAnimator());
        GridLayoutManager mLayoutManager = new GridLayoutManager(this, 2);
        rvListLike.setLayoutManager(mLayoutManager);
        ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(this, R.dimen.item_offset);
        rvListLike.addItemDecoration(itemDecoration);
        rvListLike.setNestedScrollingEnabled(false);
        oftenBuyAdapter = new OftenBuyAdapter(PaySuccessfulNewActivity.this, mList);
        oftenBuyAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (mList.get(position).getGoods_status() == 0) {
                    Intent intent = new Intent(PaySuccessfulNewActivity.this, ShopActivity.class);
                    intent.putExtra("shop_id", mList.get(position).getStore_id() + "");
                    intent.putExtra("good_id", mList.get(position).getId() + "");
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(PaySuccessfulNewActivity.this, XFDGoodsDeatailsActivity.class);
                    intent.putExtra("good_id", mList.get(position).getId() + "");
                    startActivity(intent);
                }
            }
        });
        rvListLike.setAdapter(oftenBuyAdapter);
        if (null != getIntent().getStringExtra("money")) {
            if (getIntent().getStringExtra("money").contains("¥")){
                tvTotalMoney.setText( getIntent().getStringExtra("money"));
            }else {
                tvTotalMoney.setText("¥" + getIntent().getStringExtra("money"));
            }
        }
        if (null != getIntent().getStringExtra("time")) {
            tvExpectTime.setText(getIntent().getStringExtra("time"));
        }
        setThemColor();
        mPresenter.myInterestGood(true, true, type,page + "", "" + limit);


        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.myInterestGood(true, true, type,page + "", "" + limit);
            }
        });



    }

    @Override
    protected void onResume() {
        super.onResume();
        String param = getIntent().getStringExtra("from_bankabc_param");
        Log.e("支付结果", "onResume");
        if (param != null) {
            Log.e("支付结果", param);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String param = intent.getStringExtra("from_bankabc_param");
        Log.e("支付结果onNewIntent", "onResume");
        if (param != null) {
            Log.e("支付结果onNewIntent", param);
        }
    }

    private void setThemColor() {
        if ( TYPE_IS_RETAIL==0){
            type="0";
            llState.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
            rlTitle.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
            view_green_back.setBackgroundColor(getResources().getColor(R.color.color_FB9129));
        }else {
            type="1";
            llState.setBackgroundColor(getResources().getColor(R.color.color_02C557));
            rlTitle.setBackgroundColor(getResources().getColor(R.color.color_02C557));
            view_green_back.setBackgroundColor(getResources().getColor(R.color.color_02C557));
        }
    }

    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }



    @Override
    public void showLoading() {
        ProgressDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @OnClick({R.id.iv_back, R.id.tv_back_home, R.id.tv_order_look})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
            case R.id.tv_back_home:
                finish();
                break;
            case R.id.tv_order_look:
                Intent intent = new Intent(this,AllOrderActivity.class);
                intent.putExtra("from","pay_successful");

                startActivity(intent);
                break;
        }
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onMyInterestGoodSuccess(BuyOftenGoodsBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (page == 1) {
                mList.clear();
                mRefreshLayout.setNoMoreData(false);
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.myInterestGood(false, false, type,page + "", limit + "");
                    }
                });
            }
            if (bean.getData().size() == 0) {
//                if (page==1){
//                    tvCantFind.setVisibility(View.VISIBLE);
//                }
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
                mList.addAll(bean.getData());
                oftenBuyAdapter.notifyDataSetChanged();
            }
        } else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onPosterSuccess(PosterBeen bean) {

    }

    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    @Override
    public void getUserInfoSuccess(UserInfoBean bean) {

    }

    @Override
    public void onJieGuo(GetJieguoBeen been) {
        
    }

    @Override
    protected void onDestroy() {
        mList.clear();
        mList = null;
        oftenBuyAdapter = null;
        super.onDestroy();
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }
}
